<?php

namespace App\Member\Controllers;

use Dcat\Admin\Controllers\AuthController as BaseAuthController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Dcat\Admin\Admin;
use Dcat\Admin\Layout\Content;
use App\Models\AdminUser;


class AuthController extends BaseAuthController
{

    public static $css = [
        '/register/assets/css/bootstrap.min.css',
        '/register/assets/css/helper.css',
        '/register/assets/css/main.css',
    ];


    /***
     * 模拟登陆,
     * @param $id  用户ID
     * @param $token 验证TOKEN
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\think\response\Redirect|void
     */
    public function hdlogin($id, $token)
    {
        Admin::guard()->logout();

        $newToken = md5(config('app.token') . $id . config('app.token') . date('YmdHi'));
        if (!hash_equals($token, $newToken)){
            return redirect('/hdcrm/member');
        }else{
            Admin::guard()->loginUsingId($id);
            return redirect("/");
        }
    }


    /**
     * Show the login page.
     * layui-layer-ico layui-layer-ico2
     * layui-layer-ico layui-layer-ico2
     * @return Content
     */
    public function getLogin(Content $content)
    {
        if ($this->guard()->check()) {
            return redirect($this->redirectPath());
        }

        Admin::css(static::$css);

        return $content->title(trans('handlers.login_yfdrxxxxxx'))->full()->body(view('member.login.login'));
    }


    public function postLogin(Request $request)
    {
        $credentials = $request->only([$this->username(), 'password']);
        $remember = (bool) $request->input('remember', false);

        /** @var \Illuminate\Validation\Validator $validator */
        $validator = Validator::make($credentials, [
            $this->username()   => [
                'required',
                //'exists:admin_users,username',
                function($attribute, $value, $fail){
                    if (!$user = AdminUser::whereHas('roles', function($q){
                        $q->where('id', 2);
                    })->where('username', $value)->first())
                    {
                        return $fail(trans('handlers.admin_user_zhbczxxx'));
                    }
                    if ($user->id != 1 && $user->is_lock == 0) {
                        return $fail(trans('handlers.admin_user_zhysdxxx'));
                    }
                }
            ],
            'password'          =>      'required',
            //'capt'          =>      ['required','captcha'],
        ], [
            'username.required'      =>  trans('handlers.admin_user_zhbtxxxx'),
            'password.required'      =>  trans('handlers.admin_user_mmbtxxxx'),
            //'capt.required'      =>  '验证码必填',
            //'capt.captcha'       =>  '验证码错误',
        ]);

        if ($validator->fails()) {
            return $this->validationErrorsResponse($validator);
        }

        if ($this->guard()->attempt($credentials, $remember)) {

            return $this->sendLoginResponse($request);
        }

        return $this->validationErrorsResponse([
            $this->username() => $this->getFailedLoginMessage(),
        ]);
    }


}
